const {Article} = require('../models')

module.exports = function (router) {
  // 查询文章详细信息，浏览数+1
  router.get('/c/article/:articleId', function * () {
    this.checkParams('articleId').notEmpty('缺少id')

    let {articleId} = this.params
    let article = yield Article.findById(articleId).exec()
    if (!article) this.throw('找不到该文章。')

    yield article.update({$inc: {views: 1}})

    this.body = this.apiSuccess({article})
  })

  // 查询文章详细信息
  router.get('/article/:article', function * () {
    this.checkParams('articleId').notEmpty('缺少id')

    let {articleId} = this.params
    let article = yield Article.findById(articleId).exec()
    if (!article) this.throw('找不到该文章。')

    this.body = this.apiSuccess({article})
  })
}
